package summary;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author: 海琳琦
 * @Date: 2022/3/9 10:15
 * https://leetcode-cn.com/problems/two-sum/
 */
public class Title01 {

    public static int[] twoSum(int[] nums, int target) {
        Map<Integer, Integer> hashMap = new HashMap<>();
        hashMap.put(nums[0], 0);
        for (int i = 1; i < nums.length; i++) {
            Integer integer = hashMap.get(target - nums[i]);
            if (integer!=null) {
                return new int[]{i,integer};
            }
            hashMap.put(nums[i], i);
        }
        return null;
    }






    public int[] twoSum1(int[] nums, int target) {
        Map<Integer, Integer> hash = new HashMap<>();
        for (int i = 0; i < nums.length; i++) {
            if (hash.containsKey(target - nums[i])) {
                return new int[]{hash.get(target - nums[i]), i};
            }
            hash.put(nums[i], i);
        }
        return null;
    }







    public static void main(String[] args) {
        int[] sums = {3, 2, 4};
        twoSum(sums, 6);
    }
}
